package com.example.qiniuapi;

import com.example.qiniuapi.data.QiNiuData;
import com.example.qiniuapi.data.UploadParam;
import com.example.qiniuapi.service.QiNiuApiService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController
@AllArgsConstructor
public class QiNiuController {
    public QiNiuApiService qiNiuApiService;

    @PostMapping("/api/land/upload_file")
    public ResponseEntity<QiNiuData> uploadFile(@RequestParam("directory") String directory,
                                     @RequestParam("filename") @NotBlank String filename,
                                     @RequestParam("file") @NotEmpty MultipartFile file) throws IOException {
        UploadParam param = new UploadParam();
        param.setDirectory(directory).setFilename(filename).setFile(file.getBytes());
        QiNiuData data = qiNiuApiService.uploadFile(param);
        if (data != null){
            return ResponseEntity.ok(data);
        }else {
            return ResponseEntity.status(500).body(new QiNiuData().setResponse("error"));
        }
    }
}
